﻿/*
 * NetTcpSystem.h
 */
#ifndef __GDK_NETTCPSYSTEM_H__
#define __GDK_NETTCPSYSTEM_H__

#include <gdk/net/Socket.h>
#include <gdk/net/TcpEpoll.h>
#include <gdk/net/NetTcpServer.h>
#include <gdk/net/NetTcpConnection.h>
#include <gdk/net/NetMessage.h>
#include <gdk/net/NetSystem.h>


namespace gdk
{


	///////////////////////////////////////////////
	class GDK_NET_EXPORT NetTcpSystem : public NetSystem
	{
		DECLARE_CLASS_HANDLE(NetTcpSystem)
		DECLARE_CLASS_INSTANCE1(NetTcpSystem,uint16_t)

	private:
		TcpEpoll::Handle	_tcpEpoll;
		NetTcpServerPtr		_tcpServer;
		uint16_t			_port;

	private:
		NetTcpSystem(uint16_t port);
		~NetTcpSystem(void);

	public:
		virtual bool init(void);
		virtual void exit(void);
		//-----------------------------------------------------------------

	public:
		__INLINE__ uint16_t getPort(void) const { return _port; }
		//-----------------------------------------------------------------

	private:
		bool tcp_start(NetTcpConnection *conn);
		void tcp_stop(NetTcpConnection *conn);
	private:
		NetTcpConnectionPtr createTcpConnection(void);
		//-----------------------------------------------------------------
		void onAccept(const Evt_NetTcpAccept *evt);
		void onConnectOk(const Evt_NetTcpConnectOk *evt);
		void onConnectFail(const Evt_NetTcpConnectFail *evt);
		void onClosed(const Evt_NetTcpClosed *evt);
		void onError(const Evt_NetTcpError *evt);
		void onAbort(const Evt_NetTcpAbort *evt);
		void onReceive(const Evt_NetTcpReceive *evt);
		//-----------------------------------------------------------------
		void onNetConnect(NetConnect *msg);
		void onNetClose(NetClose *msg);
		void onNetSend(NetSend *msg);
		//-----------------------------------------------------------------
	};
	typedef NetTcpSystem::Handle NetTcpSystemPtr;



} //namespace gdk



#endif //__GDK_NETTCPSYSTEM_H__
